package com.minivision.sdktestdemo.ui.view;

import android.app.Activity;
import android.widget.Toast;

import com.minivision.sdktestdemo.bean.KeyBean;
import com.minivision.sdktestdemo.bean.MusicBean;
import com.minivision.sdktestdemo.controler.ui.list.ListControler;
import com.minivision.sdktestdemo.event.DataModificationEvent;
import com.minivision.sdktestdemo.model.MusicProvider;
import com.minivision.sdktestdemo.model.Provider;
import com.minivision.sdktestdemo.utils.LoadingWindowUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by devtang on 2016/5/19.
 */
public class GroupCtrlPopContentView extends MusicCtrlPopContentViewImpl implements GroupBeanContentView, PopClickItemFactory.OnPlayListener, PopClickItemFactory.OnDeleteListener {

    private KeyBean mBean;

    private int mType;

    private ExecutorService mExecutor;

    public GroupCtrlPopContentView(Activity context) {
        super(context);
        mExecutor = Executors.newSingleThreadExecutor();
    }

    @Override
    protected List<PopClickItemFactory.PopClickItem> getPopClickList() {
        if (getClickList() == null) {
            addItemToList(PopClickItemFactory.getPlayItem(this, this));
            addItemToList(PopClickItemFactory.getDeleteItem(this, this));
        }
        return getClickList();
    }

    @Override
    public void setKeyBean(KeyBean bean, int type) {
        mType = type;
        mBean = bean;
        switch (mType) {
            case ListControler.ALBUMS:
                setTitle("专辑: " + bean.getKey());
                break;
            case ListControler.SINGERS:
                setTitle("歌手: " + bean.getKey());
                break;
            case ListControler.PATHS:
                setTitle("文件夹: " + bean.getKey());
                break;
        }
    }

    @Override
    public void onPlay() {
        DataModificationEvent event = new DataModificationEvent(mBean.getmListSongs(), DataModificationEvent.ACTION_GROUP_PLAY);
        EventBus.getDefault().post(event);
    }

    private boolean mFlag;

    private DataModificationEvent mEvent;

    @Override
    public void onDelete(final boolean isDeletedFlag) {
        LoadingWindowUtils.showProgressBack(getContext(), true);
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                MusicProvider provider = MusicProvider.getInstance(getContext());
                mFlag = true;
                mEvent = mEvent == null ? new DataModificationEvent(mBean.getmListSongs(), DataModificationEvent.ACTION_DELETE) : mEvent;
                mEvent.mAction = DataModificationEvent.ACTION_DELETE;
                if (mBean.getmListSongs().size() != 1) {
                    mFlag = mFlag && (provider.remove(mBean.getmListSongs(), isDeletedFlag) != -1);
                    Provider.adjustGroup(getContext(), mBean.getmListSongs());
                } else {
                    mFlag = mFlag && (provider.remove(mBean.getmListSongs().get(0), isDeletedFlag) != -1);
                    Provider.adjustGroup(getContext(), mBean.getmListSongs().get(0));
                }
                getContext().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        LoadingWindowUtils.showProgressBack(getContext(), false);
                        if (mFlag) {
                            Toast.makeText(getContext(), "已经全部删除完毕", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getContext(), "部分歌曲未删除成功", Toast.LENGTH_SHORT).show();
                        }
                        EventBus.getDefault().post(mEvent);
                    }
                });
            }
        });
    }

    public List<String> getPaths(List<MusicBean> list) {
        List<String> res = new ArrayList<>();
        for (MusicBean bean : list) {
            res.add(bean.getmPath());
        }
        return res;
    }
}
